package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/19 11:44
 */
@Api(tags = "分类相关接口")
@RequestMapping(value = "/admin/category")
@RestController
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询分类套餐信息
     * @param categoryPageQueryDTO
     * @return
     */
    @ApiOperation(value = "分页查询分类")
    @GetMapping(value = "/page")
    public Result pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        if (categoryPageQueryDTO.getType() != null){    // 根据分类类型分页查询
            PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());   // 开始分页
            // 以下sql将会自动分页
            List<Category> categorys = categoryService.queryByCategory(categoryPageQueryDTO.getType(),categoryPageQueryDTO.getName());
            PageInfo<Category> categoryPageInfo = new PageInfo<>(categorys);
            PageResult pageResult = new PageResult(categoryPageInfo.getTotal(), categoryPageInfo.getList());
            return Result.success(pageResult);
        }
        // 开始分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        // 分页条件查询
        List<Category> categories = categoryService.pageQuery(categoryPageQueryDTO);
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
        PageResult pageResult = new PageResult(categoryPageInfo.getTotal(), categoryPageInfo.getList());
        return Result.success(pageResult);
    }

    /**
     * 新增菜品/套餐分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping
    public Result saveCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.saveCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 根据分类类型查询       该接口在增加菜品，选择菜品分类时会被调用
     * @param type
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "根据分类查询")
    public Result queryByCategory(Integer type){
       List<Category> categorys = categoryService.saveDishByCategory(type);
       return Result.success(categorys);
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "修改分类信息")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 修改分类启用还是禁用
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("修改分类启用or禁用")
    @PostMapping(value = "/status/{status}")
    public Result updateStatus(@PathVariable(value = "status") Integer status,Integer id){
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类")
    @DeleteMapping
    public Result deleteCategory(Long id){
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
